package itsm.isperp.framework.data.excel.executor;

import itsm.isperp.framework.data.excel.ExcelData;
import itsm.isperp.framework.data.excel.ImportCellDesc;
import itsm.isperp.framework.data.excel.SimpleExcelData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * 针对ExcelData的简单的处理类
 */
public class ExcelDataUtil {
	private ExcelDataUtil() {
	};

	/**
	 * 将ExcelData转化为SimpleExcelData
	 */
	public static SimpleExcelData changeExcelDataToSimple(ExcelData excelData) {
		if (excelData == null) {
			return null;
		}
		SimpleExcelData simpleExcelData = new SimpleExcelData();

		List<Map<String, ImportCellDesc>> repeatData = excelData
				.getRepeatData();
		if (repeatData != null && repeatData.size() > 0) {
			List<Map<String, Object>> simpleRepeatData = new ArrayList<Map<String, Object>>();
			for (Map<String, ImportCellDesc> map : repeatData) {
				if (map == null)
					continue;
				Map<String, Object> tmp = new HashMap<String, Object>();
				Set<String> keys = map.keySet();
				for (String key : keys) {
					if (StringUtils.isEmpty(key) || map.get(key) == null)
						continue;
					tmp.put(key, map.get(key).getFieldValue());
				}
				simpleRepeatData.add(tmp);
			}
			simpleExcelData.setRepeatData(simpleRepeatData);
		}
		return simpleExcelData;
	}
}